package com.haizhi.util;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

/**
 * Created by youfeng on 2018/3/29.
 * 时间管理类
 */
public class DateUtil {

    private static final TimeZone DEFAULT_TIME_ZONE = TimeZone.getTimeZone("Asia/Shanghai");

    /**
     * 线程安全的日期格式对象
     */
    private static final ThreadLocal<DateFormat> TIME_FORMAT = new ThreadLocal<DateFormat>() {

        private final DateFormat FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        @Override
        protected DateFormat initialValue() {
            FORMAT.setTimeZone(DEFAULT_TIME_ZONE);
            return FORMAT;
        }

    };

    public static String getCurrentTime() {
        Date date = new Date();
        return TIME_FORMAT.get().format(date);
    }

    /**
     * 线程安全的日期格式对象
     */
    private static final ThreadLocal<DateFormat> DATE_FORMAT = new ThreadLocal<DateFormat>() {

        private final DateFormat FORMAT = new SimpleDateFormat("yyyy-MM-dd");

        @Override
        protected DateFormat initialValue() {
            FORMAT.setTimeZone(DEFAULT_TIME_ZONE);
            return FORMAT;
        }

    };

    public static String getCurrentDate() {
        Date date = new Date();
        return DATE_FORMAT.get().format(date);
    }
}
